package de.tabuma.pm.externalproject.controller;

import de.tabuma.pm.*;
import de.tabuma.pm.externalproject.logik.*;
import de.tabuma.pm.externalproject.entity.*;
import de.tabuma.lib.tabumacontroller;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;

/**
 *
 * @author alefen
 */
public class remove extends tabumacontroller {

    public remove(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        if(request.getParameter(constants.cancel)!=null) {
            list list = new list(this.getServlet(), this.getParameter());
            list.navigate(context, request, response, out);
            return;
        }
        
        String externalProjectId = request.getParameter(constants.externalProjectId);
        int epidInt = Integer.valueOf(externalProjectId);
        
        if(request.getParameter(constants.removeExternalProject)!=null) {
            ExternalProject ep = ExternalProjectFacade.get(epidInt);
            request.setAttribute(constants.externalProject, ep);
            request.setAttribute(constants.externalProjectId, externalProjectId);
            request.setAttribute(constants.removeExternalProject, "true");
            return;
        }

        if(request.getParameter(constants.removeExternalProject1)!=null) {
            ExternalProject ep = ExternalProjectFacade.get(epidInt);
            String message = "External Project &quot;{1}&quot; successfully removed!";
            message = message.replaceAll("\\{1\\}", ep.getName());
            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();
            ExternalProjectFacade.remove(epidInt, user.getEmail());
            request.setAttribute(constants.externalProjectSuccessMessage, message);
            list list = new list(this.getServlet(), this.getParameter());
            list.navigate(context, request, response, out);
        }
    }
}
